import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:riki_login/api/base_api.dart';
import 'package:riki_login/entity/staff_login_entity.dart';
import 'package:riki_login/entity/user_login_entity.dart';
import 'package:flutter/material.dart';

///
///作者：范玉辉
///创建时间：2021/8/17
///说明：

///国美登陆后获取账户信息 (user)
class OauthGomeLoginApi extends BaseApi<UserLoginEntity> {
  late String token;

  OauthGomeLoginApi(BuildContext context) : super(context);

  Map<String, dynamic> params(String token, {String? idfa, imei, androidId, oaid}) {
    this.token = token;
    return {'clientType': getSendPlatformType(), 'advert': advertParams(idfa: idfa, imei: imei, oaid: oaid)};
  }

  @override
  String accessToken() {
    return this.token;
  }

  @override
  HttpMethod get httpMethod => HttpMethod.post;

  @override
  String apiMethod(params) => 'app/oauth/gome/login';

  @override
  UserLoginEntity? onExtractResult(resultData, HttpData<UserLoginEntity> data) {
    return resultData != null ? UserLoginEntity.fromJson(resultData) : null;
  }
}

///国美-查询用户信息 (User)
class OauthGomeInfoApi extends BaseApi<UserLoginEntity> {
  OauthGomeInfoApi(BuildContext context) : super(context);

  @override
  HttpMethod get httpMethod => HttpMethod.get;

  @override
  String apiMethod(params) => 'app/oauth/gome/info';

  @override
  UserLoginEntity? onExtractResult(resultData, HttpData<UserLoginEntity> data) {
    return resultData != null ? UserLoginEntity.fromJson(resultData) : null;
  }
}

/// 说明: 1、通知后台登录成功接口 2、应用唤起的时候需要通知后台 (user)
class UserLoginRecordApi extends BaseApi<UserLoginEntity> {
  UserLoginRecordApi(BuildContext context) : super(context);

  @override
  HttpMethod get httpMethod => HttpMethod.post;

  @override
  String apiMethod(params) => 'app/userLoginRecord/insert';

  @override
  UserLoginEntity? onExtractResult(resultData, HttpData<UserLoginEntity> data) {
    return resultData != null ? UserLoginEntity.fromJson(resultData) : null;
  }
}

///密码登录 (staff)
class StaffPassLoginApi extends BaseApi<StaffLoginEntity> {
  StaffPassLoginApi(BuildContext context) : super(context);

  @override
  HttpMethod get httpMethod => HttpMethod.post;

  @override
  String apiMethod(params) => 'app/oauth/serverApp/login';

  @override
  StaffLoginEntity? onExtractResult(resultData, HttpData<StaffLoginEntity> data) {
    return resultData != null ? StaffLoginEntity.fromJson(resultData) : null;
  }
}

///密码登录 (User)
class UserPassLoginApi extends BaseApi<UserLoginEntity> {
  UserPassLoginApi(BuildContext context) : super(context);

  @override
  HttpMethod get httpMethod => HttpMethod.post;

  @override
  String apiMethod(params) => 'app/oauth/login';

  @override
  UserLoginEntity? onExtractResult(resultData, HttpData<UserLoginEntity> data) {
    return resultData != null ? UserLoginEntity.fromJson(resultData) : null;
  }
}

/// 获取用户信息 (staff)
class OauthUserApi extends BaseApi<StaffLoginEntity> {
  OauthUserApi(BuildContext context) : super(context);

  @override
  HttpMethod get httpMethod => HttpMethod.get;

  @override
  String apiMethod(params) => 'app/oauth/user';

  @override
  StaffLoginEntity? onExtractResult(resultData, HttpData<StaffLoginEntity> data) {
    return resultData != null ? StaffLoginEntity.fromJson(resultData) : null;
  }
}

/// 获取哪个平台
int getSendPlatformType() {
  return Platform.isAndroid ? 1 : 2;
}

Map<String, dynamic> advertParams({String? idfa, imei, androidId, oaid}) =>
    {'os': _os, 'idfa': idfa, 'imei': imei, 'androidId': androidId, 'oaid': oaid};

//安卓0  ios1  其他3
String _os = Platform.isAndroid ? '0' : (Platform.isIOS ? '1' : '2');
